using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using Framework.StateMechine;
using UnityEngine;

namespace Game.Hotfix
{
    public class HotfixVerifyChecksum : StateBase
    {
        private Game.Hotfix.Hotfix hotfix;
        
        public override async UniTask OnEnter(StateArgs _args)
        {
            Debug.Log("[HotfixVerifyChecksum] OnEnter.");
            
            hotfix = stateMachine.owner as Game.Hotfix.Hotfix;
            await base.OnEnter(_args);

            Verify();
            
            stateMachine.Change(HotfixState.DownloadRemote2Local);
        }

        private void Verify()
        {
            // 断点续传
            // tempDownloadRecord.recordVersion(launcherNetInfo.clientVersion);

            foreach (KeyValuePair<string, ChecksumData> elm in hotfix.remoteChecksum)
            {
                // if (tempDownloadRecord.isRecorded(elm.Value)) // 判断是否下载过了
                // {
                //     continue;
                // }

                if (!hotfix.localChecksum.ContainsKey(elm.Value.filename))
                {
                    hotfix.downLoadList.Add(elm.Value);
                    hotfix.downloadSize += elm.Value.filesize;
                }
                else if (elm.Value.checksum != hotfix.localChecksum[elm.Key].checksum)
                {
                    hotfix.downLoadList.Add(elm.Value);
                    hotfix.downloadSize += elm.Value.filesize;
                }
            }

            Debug.Log("[HotfixVerifyChecksum] request finished and " + hotfix.downLoadList.Count + " files need download");
            for (int i = 0; i < hotfix.downLoadList.Count; i++)
            {
                Debug.Log("[HotfixVerifyChecksum] need download filename: " + hotfix.downLoadList[i].filename + ", filesize: " + hotfix.downLoadList[i].filesize);
            }
        }
    }
}